SELECT* FROM dbo.TinMacCa
Go

CREATE TRIGGER [dbo].[TRG_IDENTITY_TinMacCa] ON [dbo].TinMacCa INSTEAD OF INSERT AS
BEGIN
	DECLARE @MaTinMacCa CHAR(10)
	DECLARE @NgayGui DATETIME
	DECLARE @MaTaiKhoanGui CHAR(10)
	DECLARE @MaTinBan CHAR(10)
	DECLARE @MaTaiKhoanNhan CHAR(10)
	DECLARE @SoLuongMua INT
	DECLARE @GiaNiemYet INT
	DECLARE @GiaDeNghiTra INT
	DECLARE @TinNhan NVARCHAR(100)
	DECLARE @HanTraLoi DATETIME
	DECLARE @ChoPhepMacCa BIT
	
	SET @NgayGui =(SELECT NgayGui FROM INSERTED)
	SET @MaTaiKhoanGui =(SELECT MaTaiKhoanGui FROM INSERTED)
	SET @MaTinBan =(SELECT MaTinBan FROM INSERTED)
	SET @MaTaiKhoanNhan =(SELECT MaTaiKhoanNhan FROM INSERTED)
	SET @SoLuongMua =(SELECT SoLuongMua FROM INSERTED)
	SET @GiaNiemYet =(SELECT GiaNiemYet FROM INSERTED)
	SET @GiaDeNghiTra =(SELECT GiaDeNghiTra FROM INSERTED)
	SET @TinNhan =(SELECT TinNhan FROM INSERTED)
	SET @HanTraLoi =(SELECT HanTraLoi FROM INSERTED)
	SET @ChoPhepMacCa =(SELECT ChoPhepMacCa FROM INSERTED)
	
	SET @MaTinMacCa =(select top 1 MaTinMacCa from dbo.TinMacCa order by MaTinMacCa desc)
	if @MaTinMacCa IS NULL
		begin
			Set @MaTinMacCa = 'MC00000001'
		end
	else
		BEGIN
			set @MaTinMacCa = Convert(int,SubString(@MaTinMacCa,3,10)) + 1
			if len(@MaTinMacCa) = 1
				begin
					set @MaTinMacCa = 'MC0000000' + @MaTinMacCa
				end
			else if	len(@MaTinMacCa) = 2
				begin
					set @MaTinMacCa='MC000000' + @MaTinMacCa
				end
			else if	len(@MaTinMacCa) = 3
				BEGIN
					set @MaTinMacCa='MC00000' + @MaTinMacCa
				END
			ELSE IF LEN(@MaTinMacCa) = 4
				BEGIN
					set @MaTinMacCa='MC0000' + @MaTinMacCa
				END
			ELSE IF LEN(@MaTinMacCa) = 5
				BEGIN
					set @MaTinMacCa='MC000' + @MaTinMacCa
				END
			ELSE IF LEN(@MaTinMacCa) = 6
				BEGIN
					set @MaTinMacCa='MC00' + @MaTinMacCa
				END
			ELSE IF LEN(@MaTinMacCa) = 7
				BEGIN
					set @MaTinMacCa='MC0' + @MaTinMacCa
				END
			ELSE IF LEN(@MaTinMacCa) = 8
				BEGIN
					set @MaTinMacCa='MC' + @MaTinMacCa
				END
		END
	Insert Into TinMacCa(MaTinMacCa, NgayGui, MaTaiKhoanGui, MaTinBan, MaTaiKhoanNhan, SoLuongMua, GiaNiemYet, GiaDeNghiTra, TinNhan, HanTraLoi, ChoPhepMacCa)
	Values(@MaTinMacCa, @NgayGui, @MaTaiKhoanGui, @MaTinBan, @MaTaiKhoanNhan, @SoLuongMua, @GiaNiemYet, @GiaDeNghiTra, @TinNhan, @HanTraLoi, @ChoPhepMacCa)
End
GO

ALTER TRIGGER [dbo].[TRG_IDENTITY_GiaoDichNapTien] ON [dbo].GiaoDichNapTien INSTEAD OF INSERT AS
BEGIN
	DECLARE @MaGiaoDich CHAR(10)
	DECLARE @MaKhachHang CHAR(10)
	DECLARE @ThoiGianNap DATETIME
	DECLARE @SoTienNap INT
	DECLARE @HinhThucNap NVARCHAR(30)
	
	SET @MaKhachHang =(SELECT MaKhachHang FROM INSERTED)
	SET @ThoiGianNap =(SELECT ThoiGianNap FROM INSERTED)
	SET @SoTienNap =(SELECT SoTienNap FROM INSERTED)
	SET @HinhThucNap =(SELECT HinhThucNap FROM INSERTED)
	
	SET @MaGiaoDich =(select top 1 MaGiaoDich from dbo.GiaoDichNapTien order by MaGiaoDich desc)
	if @MaGiaoDich IS NULL
		begin
			Set @MaGiaoDich = 'NT00000001'
		end
	else
		BEGIN
			set @MaGiaoDich = Convert(int,SubString(@MaGiaoDich,3,10)) + 1
			if len(@MaGiaoDich) = 1
				begin
					set @MaGiaoDich = 'NT0000000' + @MaGiaoDich
				end
			else if	len(@MaGiaoDich) = 2
				begin
					set @MaGiaoDich='NT000000' + @MaGiaoDich
				end
			else if	len(@MaGiaoDich) = 3
				BEGIN
					set @MaGiaoDich='NT00000' + @MaGiaoDich
				END
			ELSE IF LEN(@MaGiaoDich) = 4
				BEGIN
					set @MaGiaoDich='NT0000' + @MaGiaoDich
				END
			ELSE IF LEN(@MaGiaoDich) = 5
				BEGIN
					set @MaGiaoDich='NT000' + @MaGiaoDich
				END
			ELSE IF LEN(@MaGiaoDich) = 6
				BEGIN
					set @MaGiaoDich='NT00' + @MaGiaoDich
				END
			ELSE IF LEN(@MaGiaoDich) = 7
				BEGIN
					set @MaGiaoDich='NT0' + @MaGiaoDich
				END
			ELSE IF LEN(@MaGiaoDich) = 8
				BEGIN
					set @MaGiaoDich='NT' + @MaGiaoDich
				END
		END
	Insert Into GiaoDichNapTien(MaGiaoDich, MaKhachHang, ThoiGianNap, SoTienNap, HinhThucNap)
	Values(@MaGiaoDich, @MaKhachHang, @ThoiGianNap, @SoTienNap, @HinhThucNap)
End
GO

--DROP TRIGGER [dbo].[TRG_IDENTITY_TinMacCa]
--DROP TRIGGER [dbo].[TRG_IDENTITY_GiaoDichNapTien]